package com.fndsoft.zheshangmeap.util;

import java.util.List;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.Display;
import android.view.WindowManager;

import com.fndsoft.zheshangmeap.ui.base.BaseAc;
import com.fndsoft.zheshangmeap.ui.base.BaseApp;

/**
 * @author carlos
 * @version 0.1
 * @created 06-Aug-2012 10:15:44 AM
 */
public class PhoneUtil extends BaseUtils {

	public static int getScreenWidth() {
		Display display = ((WindowManager) BaseApp.AppContext
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		return display.getWidth();
	}
	/**
	 * 获取IMEI
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2013-8-14 下午5:03:01
	 * @return
	 */
	public static String getIMEI() {
		TelephonyManager mTm = (TelephonyManager) BaseApp.AppContext.getSystemService(Context.TELEPHONY_SERVICE);
		return mTm.getDeviceId();
	}
	
	/**
	 * 获取手机号
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2013-8-14 下午5:02:37
	 * @return
	 */
	public static String getPhoneNumber() {
		TelephonyManager mTm = (TelephonyManager) BaseApp.AppContext
				.getSystemService(Context.TELEPHONY_SERVICE);

		String telNumber = mTm.getLine1Number();
		return telNumber;
	}

	/**
	 * 获取当前的手机的MAC地址
	 * 
	 * @auther david 2013-11-28 下午5:27:23 String
	 */
	public static String getPhoneMacAddress() {
		WifiManager mWifiManager = (WifiManager) BaseApp.AppContext
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
		String macAddress = wifiInfo.getMacAddress();
		return macAddress;
	}

	public static int getScreenHeight() {
		Display display = ((WindowManager) BaseApp.AppContext
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		return display.getHeight();
	}


	/**
	 * org的屏幕状态是否跟当前的一样
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2012-8-14 上午10:50:06
	 * @param org
	 * @param currentOrientation
	 * @return
	 */
	public static boolean isCurrentOrientation(String org) {
		if (null == org) {
			return true;
		}
		int outOrientation = org.contains("portrait") ? Configuration.ORIENTATION_PORTRAIT
				: Configuration.ORIENTATION_LANDSCAPE;
		int currentOrientation = BaseAc.currentActivity.getResources()
				.getConfiguration().orientation;
		return currentOrientation == outOrientation;
	}

	/**
	 * 查找手机是否已经安装了这个应用
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2012-9-16 下午6:44:40
	 * @param packageName
	 * @return
	 */
	public static Intent getlaunchIntent(String packageName) {
		if (TextUtils.isEmpty(packageName)) {
			return null;
		}
		Intent mLaunchIntent = null;
		mLaunchIntent = BaseApp.AppContext.getPackageManager()
				.getLaunchIntentForPackage(packageName);
		boolean enabled = false;
		if (mLaunchIntent != null) {
			List<ResolveInfo> list = BaseApp.AppContext.getPackageManager()
					.queryIntentActivities(mLaunchIntent, 0);
			if (list != null && list.size() > 0) {
				enabled = true;

			}
		}
		if (enabled) {
			return mLaunchIntent;
		}
		return null;
	}

	/**
	 * 回到桌面
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2012-9-17 下午12:03:36
	 */
	public static void goToHomeScreen() {
		try {
			Intent mHomeIntent = new Intent(Intent.ACTION_MAIN);
			mHomeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			mHomeIntent.addCategory(Intent.CATEGORY_HOME);
			BaseAc.currentActivity.startActivity(mHomeIntent);

		} catch (Exception e) {
			// 注意,在乐phone会抛异常,这是因为乐phone改了系统底层
			try {
				Intent mHomeIntent = new Intent(Intent.ACTION_MAIN, null);
				mHomeIntent.putExtra("GOHOME", "GOHOME");
				mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
						| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
				mHomeIntent.setClassName("com.android.launcher",
						"com.android.launcher.HomeScreen");
				BaseAc.currentActivity.startActivity(mHomeIntent);
			} catch (Exception e1) {
			}

		}
	}

	/**
	 * 启动这个游戏
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2012-9-19 上午10:34:36
	 * @param packageName
	 */
	public static void launchGame(String packageName) {
		Intent intent = getlaunchIntent(packageName);
		BaseAc.currentActivity.startActivity(intent);
		// Intent.ACTION_PACKAGE_ADDED
	}

	/**
	 * 根据路径安装apk
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2012-9-19 上午10:38:25
	 * @param string
	 */
	public static void installAPK(String filePath) {
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setDataAndType(Uri.parse("file://" + filePath),
				"application/vnd.android.package-archive");
		BaseAc.currentActivity.startActivity(i);
	}

	/**
	 * 拨打电话
	 * 
	 * @author LiangXiangJu
	 * @param phoneNum
	 *            电话号码
	 */
	public static void callPhone(String phoneNum) {
		Intent phoneIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
				+ phoneNum));
		// 启动Intent
		BaseAc.currentActivity.startActivity(phoneIntent);
	}

	/**
	 * 发送短信
	 * 
	 * @author LiangXiangJu
	 * @param phoneNum
	 *            电话号码
	 * @param msg
	 *            短信内容
	 */
	public static void sendSmsMsg(String phoneNum, String msg) {
		// 消息管理器
		SmsManager smsManager = SmsManager.getDefault();
		// 发送短信
		PendingIntent pi = PendingIntent.getActivity(BaseAc.currentActivity, 0,
				new Intent(), 0);
		smsManager.sendTextMessage(phoneNum, null, msg, pi, null);
	}

	/**
	 * 发送Email
	 * 
	 * @author LiangXiangJu
	 * @param receiver
	 *            接收人
	 * @param subject
	 *            主题
	 * @param body
	 *            邮件正文
	 */
	public static void sendEmail(String[] receiver, String subject, String body) {
		Intent myIntent = new Intent(android.content.Intent.ACTION_SEND);
		// 设置内容格式
		myIntent.setType("plain/text");
		// 设置收件人
		myIntent.putExtra(android.content.Intent.EXTRA_EMAIL, receiver);
		// 设置主题
		myIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
		// 设置邮件正文
		myIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
		// 通过选择系统已经邮件服务，发送邮件
		BaseAc.currentActivity.startActivity(Intent.createChooser(myIntent,
				"发送邮件"));
	}
}